<?php
/**
 *
 * 微博视频的数据库逻辑操作类
 *
 *
*/


class Video
{
	
	function parse($url) {
		$ret = array();
			
		$url = trim($url);
		if(!$url) {
			return $ret;
		}
			
		$urls = parse_url($url);
		$host = strtolower(trim($urls["host"]));
		if(!$host) {
			return $ret;
		}
			
		$ret = $this->_get_parse_code($host,$urls,$url);

	
		return $ret;
	}
	
function _get_parse_code($host,$urls,$url) {

		$vhconfs = array(
        	"youku.com" => array("p"=>"/\/id_([\w\d\=]+)\.html/","c"=>"utf-8","tp"=>"/\<title\>(.+?)[—\-\_].*\</i","ip"=>"~(?:(?:\_href\s*\=\s*[\'\"]iku\:\/\/.+?)|(?:\&pic\s*\=\s*)|(?:\&screenshot\s*\=\s*))(http\:\/\/[\w\d]+\.ykimg\.com\/[^\|\'\"\s]+)~i", "ppp"=>1),
        	"sina.com.cn" => array("p"=>"/\/(\d+)\-(\d+)\.html/","c"=>"utf-8","tp"=>"/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/","ip"=>"~pic\s*[\:\=]\s*[\'\"](http\:\/\/[^\"]+?\.(?:jpg|jpeg|gif|png|bmp))[\'\"]~i", "ppp"=>1),
        	"tudou.com" => array("p"=>"/view\/([\w\d\-\_]+)/","c"=>"gbk","tp"=>"/\<title\>(.+?)(?:[\-\_].*?)?\<\/title\>/","ip"=>"/(?:bigItemUrl|pic)\s*[\=\:]\s*[\'\"]([^\'\"]+?)[\'\"]/i", "ppp"=>1, "gzip"=>1,),
        	"ku6.com" => array("p"=>"/\/([\w\-\_\.]+)\.html/","c"=>"gbk","tp"=>"/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i","ip"=>"~(?:(?:\<span class=\"s_pic\"\>)|(?:cover\s*[\:\=]\s*[\'\"]))(http\:\/\/[^\"\<]+?\.(?:jpg|jpeg|gif|png|bmp))~i", "ppp"=>0, ),
        	"sohu.com" => array("p"=>"/\/([\d]+)\/?$/","c"=>"gbk","tp"=>"/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i","ip"=>"/(?:(?:[\'\"]og\:image[\'\"]\s*content)|(?:cover))\s*[\:\=]\s*[\'\"]([^\'\"]+?)[\'\"]/", "ppp"=>1),
        	"mofile.com" => array("p"=>"/\/(\w+)\/?$/","c"=>"utf-8","tp"=>"/\<title\>(.+?)\s*[\-\_].*?\<\/title\>/i","ip"=>"/thumbpath=\"(.*?)\";/i",),
		);
		foreach($vhconfs as $k=>$v) {
			if(false!==strpos($host,$k)) {
				$return = array();
				if(preg_match($v["p"],$url,$m) && $m[1]) {
					$return["id"] = $m[1];
					$return["host"] = $k;
					$return["url"] = $url;
					$html = '';
					if(($v["tp"] || $v["ip"]) && ($html = @file_get_contents($url))) {
						if(isset($v["gzip"])) {
							$html = $this->gzdecode($html);
						}
						
						$html = $this->array_iconv($v["c"],"utf-8", $html);

						if($v["tp"] && preg_match($v["tp"],$html,$m) && $m[1]) {
							$return["title"] = $m[1];
						}

						if($v["ip"] && preg_match($v["ip"],$html,$m) && $m[1]) {
							$return["image_src"] = $return["image"] = $m[1];
						}
					}
				} elseif($v["ppp"]) {
					if("tudou.com" == $k) {
						if(false!==strpos($url, "play/") && ($html = @file_get_contents($url))) {						
							if(isset($v["gzip"])) {
								$html = $this->gzdecode($html);
							}
							$html = $this->array_iconv($v["c"], "utf-8", $html);

							$iid = "";
							$icode = "";

							if(preg_match("~(?:(?:[\?\&\#]iid\=)|(?:\d+i))(\d+)~",$url,$m) && $m[1]) {
								$iid = $m[1];
							} elseif(preg_match("~(?:(?:\,iid\s*=)|(?:\,defaultIid\s*=)|(?:\.href\)\s*\|\|))\s*(\d+)~",$html,$m) && $m[1]) {
								$iid = $m[1];
							}

							if(preg_match("~".$iid.".*?icode\s*[\:\=]\s*(?:[^\'\"]*?)[\'\"]([\w\d\-\_]+)[\'\"]~s",$html,$m) && $m[1]) {
								$icode = $m[1];
							}

							if($icode) {
								$return["id"] = $icode;
								$return["url"] = $url;
								$return["title"] = "";

								if($v["tp"] && preg_match($v["tp"],$html,$m) && $m[1]) {
									$return["title"] = $m[1];
								}
								if(preg_match("~".$iid.".*?(?:\,kw)\s*[\:\=]\s*[\'\"]([^\'\"]+?)[\'\"]~s",$html,$m) && $m[1]) {
									if(false !== strpos($m[1], $return["title"])) {
										$return["title"] = $m[1];
									} else {
										$return["title"] .= " " . $m[1];
									}
								}

								if(preg_match("~".$iid.".*?(?:bigItemUrl|pic)\s*[\:\=]\s*[\'\"]([^\'\"]+?)[\'\"]~is",$html,$m) && $m[1]) {
									$return["image_src"] = $return["image"] = $m[1];
								}
							}
						}
					} elseif("youku.com" == $k) {
						if(preg_match("~\/v\_playlist\/.+?\.htm~",$url) && ($html = @file_get_contents($url))) {
							if(isset($v["gzip"])) {
								$html = $this->gzdecode($html);
							}
							$html = $this->array_iconv($v["c"], "utf-8", $html);

							$id = "";
							if(preg_match("~\_href\s*\=\s*[\'\"]iku\:\/\/.+?http\:\/\/v\.youku\.com\/v\_show\/id\_([\w\d]+)\.htm~i", $html, $m) && $m[1]) {
								$id = $m[1];
							}

							if($id) {
								$return["id"] = $id;
								$return["url"] = $url;
								$return["title"] = "";

								if($v["tp"] && preg_match($v["tp"],$html,$m) && $m[1]) {
									$return["title"] = $m[1];
								}
								if(preg_match("~\_href\s*\=\s*[\'\"]iku\:\/\/.+?". $id .".+?\|([\%\w\d]+?)\|~", $html, $m) && $m[1]) {
									$m[1] = $this->array_iconv($v["c"], "utf-8", urldecode($m[1]));

									$return["title"] .= " " . $m[1];
								}

								if($v["ip"] && preg_match($v["ip"],$html,$m) && $m[1]) {
									$return["image_src"] = $return["image"] = $m[1];
								}
							}
						}
					} elseif("sina.com.cn" == $k) {
						if(preg_match("/video\.sina\.com\.cn\/.+?\/([\d\-]+)\.html/",$url) && ($html=@file_get_contents($url))) {
							if(isset($v["gzip"])) {
								$html = $this->gzdecode($html);
							}
							$html = $this->array_iconv($v["c"], "utf-8", $html);

							$id = "";
							if(preg_match("~vid\s*[\:]\s*[\'\"]([^\'\"]+?)[\'\"]~i", $html, $m) && $m[1]) {
								$id = $m[1];
							}

							if($id) {
								$return["id"] = $id;
								$return["url"] = $url;
								$return["title"] = "";

								if($v["tp"] && preg_match($v["tp"],$html,$m) && $m[1]) {
									$return["title"] = $m[1];
								}

								if($v["ip"] && preg_match($v["ip"],$html,$m) && $m[1]) {
									$return["image_src"] = $return["image"] = $m[1];
								}
							}
						}
					} elseif("sohu.com" == $k) {
						if(preg_match("~tv\.sohu\.com\/([\w]+\/)+[\w]+\.shtml~i", $url) && ($html=@file_get_contents($url))){
							if(isset($v["gzip"])){
								$html = $this->gzdecode($html); 
							}
							$html = $this->array_iconv($v["c"], "utf-8", $html);
							
							$id = "";
							if(preg_match("~vid\s*[\:\=]\s*[\'\"]([^\'\"]+?)[\'\"]~i", $html, $m) && $m[1]) {
								$id = $m[1];
							}
							if($id){
								$return["id"] = $id;
								$return["url"] = $url;
								$return["host"] = "tv.sohu.com";
								$return["title"] = "";
								if($v["tp"] && preg_match($v["tp"], $html, $m) && $m[1]){
									$return["title"] = $m[1];
								}
								if($v["ip"] && preg_match($v["ip"], $html, $m) && $m[1]){
									$return["image_src"] = $return["image"] = $m[1];
								}
							}
						}
					}

					if($return && !isset($return["host"])) {
						$return["host"] = $k;
					}
				}

				$ret = $return;
				break;
			}
		}
		return $return;
	}
	
	
	function _parse_video_image($image_url)
	{
		$return = '';
		if ($image_url)
		{
			$img_src_md5 = md5($image_url);
			$img_path = 'theme/img/videoimg/' ;
			$img_name = $img_src_md5[2] . $img_src_md5[3] . crc32($image_url) . '.jpg';
			$video_img = $img_path . $img_name;
	
			if (!is_file($video_img) && ($temp_image = @file_get_contents($image_url)))
			{
				if (!is_dir($img_path))  mkdir($img_path,777);	
				@file_put_contents($video_img,$temp_image);
			}
			$return = $video_img;
		}
		return $return;
	}
	
	function gzdecode ($data){
		$flags = ord(substr($data, 3, 1));
		$headerlen = 10;
		$extralen = 0;
		$filenamelen = 0;
		if ($flags & 4){
			$extralen = unpack('v' , substr($data, 10, 2));
			$extralen = $extralen[1];
			$headerlen += 2 + $extralen;
		}
		if ($flags & 8) 		$headerlen = strpos($data, chr(0), $headerlen) + 1;
		if ($flags & 16) 		$headerlen = strpos($data, chr(0), $headerlen) + 1;
		if ($flags & 2) 		$headerlen += 2;
		$unpacked = @gzinflate(substr($data, $headerlen));
		if ($unpacked === FALSE)
			$unpacked = $data;
		return $unpacked;
	}
	
	function array_iconv($in_charset,$out_charset,$array) {
		if(is_array($array)) {
			foreach($array as $key=>$val) {
				$key = iconv($in_charset, $out_charset, $key);
				$array[$key] = $this->array_iconv($in_charset,$out_charset,$val);
			}
		} else $array = iconv($in_charset,$out_charset,$array);
		return $array;
	}
}

?>